//package com.member.shop;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.member.common.utils.DateUtils;
//import com.member.common.utils.StringUtils;
//import com.member.shop.domain.SysBizActivityEnroll;
//import com.member.shop.domain.SysBizOrder;
//import com.member.shop.service.ISysBizActivityEnrollService;
//import com.member.shop.service.ISysBizOrderService;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.Redisson;
//import org.redisson.api.RBlockingQueue;
//import org.redisson.api.RDelayedQueue;
//import org.redisson.api.RedissonClient;
//import org.redisson.config.Config;
//import org.redisson.config.SingleServerConfig;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.util.Date;
//import java.util.concurrent.TimeUnit;
//
//@Component
//@Slf4j
//public class RedissonDelayQueue {
//
//    private RDelayedQueue<String> delayQueue;
//    private RBlockingQueue<String> blockingQueue;
//
//    @Resource
//    private ISysBizOrderService sysBizOrderService;
//    @Resource
//    private ISysBizActivityEnrollService sysBizActivityEnrollService;
//
//    @PostConstruct
//    public void init() {
//        initDelayQueue();
//        startDelayQueueConsumer();
//    }
//
//    private void initDelayQueue() {
//        Config config = new Config();
//        SingleServerConfig serverConfig = config.useSingleServer();
//        serverConfig.setAddress("redis://localhost:6379");
//        RedissonClient redissonClient = Redisson.create(config);
//
//        blockingQueue = redissonClient.getBlockingQueue("ORDER_TIME_OUT");
//        delayQueue = redissonClient.getDelayedQueue(blockingQueue);
//    }
//
//    private void startDelayQueueConsumer() {
//        new Thread(() -> {
//            while (true) {
//                try {
//                    //将到期的数据取出来，如果一直没有到期数据，就一直等待。
//                    String key = blockingQueue.take();
//                    if (key.startsWith("ORDER_")) {
//                        String payId = key.replace("ORDER_", StringUtils.EMPTY);
//                        SysBizOrder sysBizOrder = sysBizOrderService.getOne(new LambdaQueryWrapper<SysBizOrder>().eq(SysBizOrder::getPaymentId, payId));
//                        if (sysBizOrder != null && sysBizOrder.getStatus() == 10) {
//                            Date nowDate = DateUtils.getNowDate();
//                            sysBizOrder.setCancelTime(nowDate);
//                            sysBizOrder.setCancelReason("支付超时");
//                            sysBizOrder.setUpdateTime(nowDate);
//                            sysBizOrder.setStatus(60);
//                            // 关闭订单
//                            sysBizOrderService.cancelOrder(sysBizOrder);
//                            log.info("{}订单支付超时", key);
//                        }
//                    }
//                    if (key.startsWith("ACTIVITY_")) {
//                        log.info("{}活动报名订单支付超时", key);
//                        String payId = key.replace("ACTIVITY_", StringUtils.EMPTY);
//                        SysBizActivityEnroll sysBizActivityEnroll = sysBizActivityEnrollService.getOne(new LambdaQueryWrapper<SysBizActivityEnroll>().eq(SysBizActivityEnroll::getPayId, payId));
//                        if (sysBizActivityEnroll != null && sysBizActivityEnroll.getPayStatus() == 0) {
//                            sysBizActivityEnroll.setPayStatus(2);
//                            sysBizActivityEnrollService.updateById(sysBizActivityEnroll);
//                            log.info(key + "活动报名订单支付超时结束");
//                        }
//                    }
//                    log.info(key);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }, "ORDER_TIME_OUT-Consumer").start();
//    }
//
//    public void offerTask(String task, long seconds) {
//        log.info("添加延迟任务:{} 延迟时间:{}s", task, seconds);
//        delayQueue.offer(task, seconds, TimeUnit.SECONDS);
//    }
//
//}
